﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Xin478.CSWork.Common;
using Xin478.MySocket;

namespace Xin478.CSWork.Client
{

    public partial class RemindListCtrl : UserControl, IMyCtrl, ICorrespondable
    {
        public enum ListType { None,Expire, NoHappen, Schedule }
        ListType listType;
        DataPageHelper<RemindInfo> dataPageHelper;
        MainForm mainForm;
        bool clickSomeRow = false;
        IClient client;

        public RemindListCtrl() { }

        public RemindListCtrl(MainForm mainForm)
        {
            InitializeComponent();
            listType = ListType.None;
            this.mainForm = mainForm;
            this.mainForm.CommandReceived += CommandReceive;
            this.remindGridView.VirtualMode = true;
            this.remindGridView.SelectionChanged += new EventHandler(remindGridView_SelectionChanged);
            this.remindGridView.CellValueNeeded += new DataGridViewCellValueEventHandler(remindGridView_CellValueNeeded);
            this.contextMenuStrip1.Opening += new CancelEventHandler(contextMenuStrip1_Opening);
            this.remindGridView.RowContextMenuStripNeeded += new DataGridViewRowContextMenuStripNeededEventHandler(remindGridView_RowContextMenuStripNeeded);
            this.remindGridView.MouseDown+= new MouseEventHandler(remindGridView_MouseDown);

            dataPageHelper = new DataPageHelper<RemindInfo>(50);
            dataPageHelper.RetrieveDataInvoker = RequestData;
        }


        void remindGridView_MouseDown(object sender, MouseEventArgs e)
        {
            clickSomeRow = false;
        }
        void remindGridView_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)
        {
            clickSomeRow = true;
            this.remindGridView.Rows[e.RowIndex].Selected=true;
        }
        void remindGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (this.remindGridView.SelectedRows.Count > 0)
            {
                int rowIndex = this.remindGridView.SelectedRows[0].Index;
                RemindInfo remindInfo = dataPageHelper.RetrieveItem(rowIndex);
                if (remindInfo != null)
                {
                    string t = remindInfo.DefiniteTime == -1 ? "首次登陆时" : " " + (remindInfo.DefiniteTime / 100).ToString() + ":" + (remindInfo.DefiniteTime % 100).ToString();

                    if (!string.IsNullOrEmpty(remindInfo.DefiniteDate))
                        label2.Text = remindInfo.DefiniteDate + t;
                    else if (remindInfo.PlanType == 0)
                        label2.Text = "每天" + t;
                    else if (remindInfo.PlanType < 0)
                    {
                        label2.Text = "每周" + weekStr(-remindInfo.PlanType) + t;
                    }
                    else
                        label2.Text = "每月" + remindInfo.PlanType.ToString() + "号" + t;

                    textBox2.Text = remindInfo.Detail;
                }
            }else
                this.remindGridView.ClearSelection();
        }
        void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.listType != ListType.Expire && remindGridView.SelectedRows.Count > 0 && clickSomeRow)
                删除右键菜单.Enabled = true;
            else
                删除右键菜单.Enabled = false;
        }

        void CommandReceive(DatagramBuilder builder, ref bool cancel)
        {
            MethodInfo mi = typeof(RemindListCtrl).GetMethod(builder.Name, BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
            if (mi != null)
            {
                mi.Invoke(this, builder.Params);
                cancel = true;
            }
        }
        void RequestData(int lowerBoundary, int rowsPerPage)
        {
            switch (this.listType)
            {
                case ListType.Expire:
                    mainForm.Send("提醒列表", lowerBoundary, rowsPerPage, "过期");
                    break;
                case ListType.NoHappen:
                    mainForm.Send("提醒列表", lowerBoundary, rowsPerPage, "未发生");
                    break;
                case ListType.Schedule:
                    mainForm.Send("提醒列表", lowerBoundary, rowsPerPage, "定期");
                    break;
            }
        }

        void remindGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            RemindInfo ri=dataPageHelper.RetrieveItem(e.RowIndex);
            if (ri == null)
            {
                if (e.ColumnIndex == 0)
                    e.Value = MainFormResource.空白;
                return;
            }
            switch (e.ColumnIndex)
            {
                case 0:
                    if (ri.DefiniteDate == null)
                        e.Value = MainFormResource.进程中;
                    else if ( string.Compare( ri.DefiniteDate.Replace("-","")+ ri.DefiniteTime.ToString() , DateTime.Now.ToString("yyyyMMddHHss"))<0)
                        e.Value = MainFormResource.已完成;
                    else
                        e.Value = MainFormResource.待接收;

                    remindGridView.Rows[e.RowIndex].Tag = ri.Id;
                    break;
                case 1:
                    e.Value = ri.AddTime;
                    break;
                case 2:
                    e.Value = ri.Detail;
                    break;
                case 3:
                    string t = ri.DefiniteTime == -1 ? "首次登陆时" : " " + (ri.DefiniteTime / 100).ToString() + ":" + (ri.DefiniteTime % 100).ToString();

                    if(!string.IsNullOrEmpty(ri.DefiniteDate))
                        e.Value = ri.DefiniteDate+t;
                    else if (ri.PlanType == 0)
                        e.Value = "每天"+t;
                    else if (ri.PlanType < 0)
                    {

                        e.Value = "每周" + weekStr(-ri.PlanType) + t;
                    }
                    else
                        e.Value = "每月" + ri.PlanType.ToString()+"号" + t;
                    break;
            }
            
        }

        string weekStr(int d)
        {
            switch (d)
            {
                case 0: return "周日";
                case 1: return "周一";
                case 2: return "周二";
                case 3: return "周三";
                case 4: return "周四";
                case 5: return "周五";
                case 6: return "周六";
            }
            return "";
        }

        public ListType ListDisplayType
        {
            get { return this.listType; }
            set
            {
                if (value == this.listType) return;

                this.listType = value;

                this.dataPageHelper.Clear();
                this.remindGridView.Rows.Clear();
                switch (this.listType)
                {
                    case ListType.NoHappen:
                        mainForm.Send("提醒数量", "未发生");
                        break;
                    case ListType.Expire:
                        mainForm.Send("提醒数量", "过期");
                        break;
                    case ListType.Schedule:
                        mainForm.Send("提醒数量", "定期");
                        break;
                }
            }
        }


        void 提醒数量(NetEventArgs e, int count, string status)
        {
            if ( (this.listType == ListType.Expire && status.Equals("过期") )
                || (status.Equals("未发生") && this.listType == ListType.NoHappen)
                || (status.Equals("定期") && this.listType == ListType.Schedule))
            {
                WinForm.DoThreadSafe(this.remindGridView, delegate() { this.remindGridView.Rows.Clear(); this.remindGridView.RowCount = count; });
            }

        }
        void 提醒列表(NetEventArgs e, int rowIndex, IList<RemindInfo> ris)
        {
            dataPageHelper.FillPage(rowIndex, ris);
            WinForm.DoThreadSafe(this.remindGridView, delegate() { this.remindGridView.Refresh(); });
        }
        void 提醒删除(NetEventArgs e, int remindId)
        {
            WinForm.DoThreadSafe(this.remindGridView, delegate()
            {
                foreach (DataGridViewRow dr in this.remindGridView.Rows)
                {
                    if (remindId == Convert.ToInt32(dr.Tag))
                    {
                        this.dataPageHelper.Clear();
                        this.remindGridView.RowCount--;
                        break;
                    }
                }
                RemindHelper.Instance.Remove(remindId);
            });
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddRemindForm addRemindForm = new AddRemindForm();
            addRemindForm.ShowDialog(mainForm);
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.dataPageHelper.Clear();
            this.remindGridView.Refresh();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.remindGridView.SelectedRows.Count > 0)
            {
                int rowIndex = this.remindGridView.SelectedRows[0].Index;
                RemindInfo ri = dataPageHelper.RetrieveItem(rowIndex);
                if (ri != null)
                    mainForm.Send("Remind.删除", ri.Id);
            }

        }
       
        public void Dispose()
        {
            mainForm.CommandReceived -= CommandReceive;
        }

        #region IMyCtrl 成员

        public IClient IClient
        {
            get
            {
                return client;
            }
            set
            {
                client = value;
            }
        }

        public void Sleep()
        {
        }

        public void WakeUp()
        {
        }

        public UnsetMethod UnsetMethod
        {
            get { return UnsetMethod.Dispose; }
        }

        #endregion
    }
}
